package at.evonfc.bl;

import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import at.evonfc.data.ChildItem;
import at.evonfc.data.ParentItem;
import at.evonfc.listofdeviceswithnfc.R;

public class MyExpandableListView extends BaseExpandableListAdapter {

	private ArrayList<ParentItem> parents;

	private Context context;

	public MyExpandableListView(Context context, ArrayList<ParentItem> items) {
		this.context = context;
		this.parents = items;
		for(ParentItem item : parents) {
			Log.v("Item", "Item: "+item.getName());
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return parents.get(groupPosition).getChildItemList().get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View view, ViewGroup parent) {
		ChildItem child = (ChildItem) getChild(groupPosition, childPosition);
		if (view == null) {
			LayoutInflater infalInflater = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			view = infalInflater.inflate(R.layout.expandlist_child_item, null);
		}
		TextView tv = (TextView) view.findViewById(R.id.tvChild);
		tv.setText(child.getText().toString());
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return parents.get(groupPosition).getChildItemList().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return parents.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return parents.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View view,
			ViewGroup parent) {
		ParentItem group = (ParentItem) getGroup(groupPosition);
		if (view == null) {
			LayoutInflater inf = (LayoutInflater) context
					.getSystemService(context.LAYOUT_INFLATER_SERVICE);
			view = inf.inflate(R.layout.expandlist_group_item, null);
		}
		TextView tv = (TextView) view.findViewById(R.id.tvGroup);
		tv.setText(group.getName());
		return view;

	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}
